<?php
namespace Business\Controller;
use Think\Controller;
use Think\AjaxPage;
use Think\Page;
class IndexController extends Controller {
    public function _initialize() {
        $this->assign('cur','index');
    }
    public function home(){
        $bid = session('bid');
        if($bid) {
            $type = I('type')?I('type'):1;
            if(i('act')=='ajax') {
                $mod = M('orders');
                $goods_mod = M('orders_goods');
                $where['store_id'] = $bid;
                if($type==2){
                    $where['status'] = 0;
                }else if($type==3){
                    $where['status'] = 1;
                }else if($type==4){
                    $where['status'] = 3;
                }else if($type==5){
                    $where['status'] = 3;
                }
                $order_list = $mod->where($where)->field('id,order_sn,status,FROM_UNIXTIME(add_time,"%Y-%m-%d (%H:%i)") as add_time')->select();
                foreach ((array)$order_list as $k => $v) {
                    $order_list[$k]['goods_list'] = $goods_mod->alias('as a')
                        ->join('left join ' . C('DB_PREFIX') . 'goods as b on b.id=a.goods_id')
                        ->join('left join ' . C('DB_PREFIX') . 'business as d on d.id=a.store_id')
                        ->join('left join ' . C('DB_PREFIX') . 'goods_spec as c on c.id=a.spec_id')
                        ->where(array('a.order_sn' => $v['order_sn']))
                        ->field('a.goods_price,a.goods_id,a.goods_number,b.goods_name,b.thumb,c.spec_name,b.keywords')
                        ->select();
                    foreach((array)$order_list[$k]['goods_list'] as $kk=>$vv){
                        $order_list[$k]['goods_list'][$kk]['left_price'] = intval($vv['goods_price']);
                        $order_list[$k]['goods_list'][$kk]['right_price'] = substr($vv['goods_price'], -3, 3);
                    }
                    $order_list[$k]['status_name'] = status_name($v['status']);
                }
                exit(json_encode(array('is_page'=>$is_page,'result'=>$order_list)));
            }
            $this->assign('type',$type);
            $this->display();
        }else {
            $this->display('login');
        }
    }
    public function index(){
        $bid = session('bid');
        if($bid){
            header('Location:'.U('/Business/User/index/'));
        }else {
            $this->display('login');
        }
    }
    public function login(){
        $mod = M('business');
        $phone = i('phone')?i('phone'):'';
        $password = i('password')?i('password'):'';
        if(!$phone){
            exit(json_encode(array('code'=>100,'msg'=>'请输入账号')));
        }
        if(!$password){
            exit(json_encode(array('code'=>100,'msg'=>'请输入密码')));
        }
        $business = $mod->where(array('phone'=>$phone))->find();
        if(!$business['id']){
            exit(json_encode(array('code'=>100,'msg'=>'请账号不存在')));
        }else if(md5($password)!==$business['password']){
            exit(json_encode(array('code'=>100,'msg'=>'密码错误')));
        }else if($business['id']){
            session('bid',$business['id']);
            session('store_name',$business['store_name']);
            exit(json_encode(array('code'=>200,'msg'=>'登录成功')));
        }
    }
    public function sign_out(){
        session('bid',null);
        session('store_name',null);
        header('Location:'.U('/Business/Index/index/'));
    }
}